package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtPackersEmploymentBinding;
import com.sgb.kjwl.model.entity.EmploymentEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.view.widget.GoodsTypePopWindow3;
import com.sgb.kjwl.view.widget.GoodsTypePopWindow4;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.packers.PackersEmploymentAdapter;
import com.sgb.kjwl.view.widget.GoodsTypePopWindow4;
import com.sgb.kjwl.view.widget.PickPopWindow1;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.viewmodel.packers.PackersEmploymentModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.inject.Inject;

/**
 * @author liwei
 * @date 2020/11/10
 * description:临时用工的列表
 * first:写在前面，在我们做全局变量的注释的时候，我们应该以javadoc的注释来完成，例如下面第一个工种的注释，这我也
 * 不是特别清除是为什么，这是阿里的一个标准
 * second:第二点，我们在做注释的时候不要写在行尾，这其实是出于一个代码的可读性出发的一个标准，我们读代码是由上往下、
 * 如果注释在后面我们则需要把当前的代码看两遍，因为假如这一句我不懂，我会先看完再看到注释，然后再会看一边
 * third:我们再以后的开发过程中，应尽量避免出现实际值，这其实是从安全性上面做了考量，实际的值是可以被任意更改的一个
 * 事物，也是再反编译过程中能轻易拿到的事物，如果这个值更换，那么逻辑就会出现错误，甚至是整个逻辑系统的崩塌，这是不可取的
 */
public class PackersEmploymentActivity extends AppActivity {

    public PackersEmploymentModel viewModel;
    private AtPackersEmploymentBinding binding;
    private PackersEmploymentAdapter adapter;

    /**
     * 工种
     */
    private List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> mWorkType;
    private PickResultEntity pickResultIndustry;
    private PickResultEntity pickResultCategory;
    private PickResultEntity pickResultWorkType;

    private PickResultEntity pickResultWorkerType;
    /**
     * 用工状态
     */
    private PickPopWindow1 mEmploymentTypePopup1;
    private PickResultEntity pickResultWorkerStatus;
    /**
     * 所在区域
     */
    private ThreeLevelAreaWindow popCity;
    private PickResultEntity pickResultProvince;
    private PickResultEntity pickResultCity;
    private PickResultEntity pickResultArea;
    private GoodsTypePopWindow4 carsTypePopWindow;
    private List<GoodsInfoTypeEntity3> carsTypeData;
    private int typePosition = -1,mHaveWorkType;
    private String salaryType;
    private String industryNo,categoryNo,workNo;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, PackersEmploymentActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    private void initFilterData(boolean isLoadMore) {
        viewModel.getEmploymentList(salaryType, pickResultIndustry.getNo(), pickResultCategory.getNo(), pickResultWorkType.getNo(),
                pickResultWorkerType.getNo(), pickResultWorkerStatus.getNo(), pickResultProvince.getNo(),
                pickResultCity.getNo(), pickResultArea.getNo(), isLoadMore);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_packers_employment);

        binding.ivEmploymentAll.setOnClickListener(this);

        viewModel.getLinkAgeWorkType();

        carsTypeData = new ArrayList<>();
        if (adapter == null) {
            adapter = new PackersEmploymentAdapter(this, null, 0);
        }

        adapter.setOnItemClick(new AdapterOnItemClick<EmploymentEntity>() {
            @Override
            public void onItemClick(EmploymentEntity employmentEntity, int position) {
                EmploymentDetailActivity.startForResult(PackersEmploymentActivity.this, employmentEntity.getNo(), employmentEntity.getProjectName(), employmentEntity.isIsApply(), 0);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                if (TextUtils.isEmpty(workNo)){

                }
                initFilterData(true);
            }
        });

        pickResultIndustry = new PickResultEntity();
        pickResultCategory = new PickResultEntity();
        pickResultWorkType = new PickResultEntity();
        pickResultWorkerType = new PickResultEntity();
        pickResultWorkerStatus = new PickResultEntity();
        pickResultProvince = new PickResultEntity();
        pickResultCity = new PickResultEntity();
        pickResultArea = new PickResultEntity();

        AddressEntity mAddress = (AddressEntity) getIntent().getSerializableExtra("entity");
        if (mAddress != null) {
            pickResultIndustry.setNo((mAddress).getNo());
            pickResultIndustry.setName((mAddress).getName());
        }

        String employmentStatus = getIntent().getStringExtra("employmentStatus");
        salaryType = getIntent().getStringExtra("salaryType");
        industryNo= getIntent().getStringExtra("industryNo");
        categoryNo = getIntent().getStringExtra("categoryNo");
        workNo = getIntent().getStringExtra("workNo");
        String provinceNo = getIntent().getStringExtra("provinceNo");
        String cityNo = getIntent().getStringExtra("cityNo");
        String areaNo = getIntent().getStringExtra("areaNo");
        String cityName = getIntent().getStringExtra("cityName");
        String industryName = getIntent().getStringExtra("industryName");
        if (!TextUtils.isEmpty(provinceNo)){
            pickResultProvince.setNo(provinceNo);
            pickResultCity.setNo(cityNo);
            pickResultCity.setName(cityName);
            pickResultArea.setNo(areaNo);

        }

        if (!TextUtils.isEmpty(industryNo)){
            pickResultIndustry.setNo(industryNo);
            pickResultCategory.setNo(categoryNo);
            pickResultWorkType.setNo(workNo);
            mHaveWorkType = 1;
            pickResultIndustry.setName(industryName);
        }

        if (!TextUtils.isEmpty(employmentStatus)){
            pickResultWorkerStatus.setNo(employmentStatus);

        }

        binding.rvPackerChangeFirstContentTitle.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                typePosition = tab.getPosition();
                if (mHaveWorkType == 1 && typePosition != 0){
                    if (!TextUtils.isEmpty(workNo)){
                        pickResultIndustry.setNo(industryNo);
                        pickResultCategory.setNo(categoryNo);
                        pickResultWorkType.setNo(workNo);
                    }

                    if (!TextUtils.isEmpty(industryNo)){
                        pickResultIndustry.setNo(industryNo);
                    }
                    initFilterData(false);
                    mHaveWorkType = -1;
                }else {
                    pickResultIndustry.setNo(mWorkType.get(tab.getPosition()).getNo());
                    pickResultCategory.setNo("");
                    pickResultWorkType.setNo("");

                    initFilterData(false);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        carsTypePopWindow = new GoodsTypePopWindow4(getActivity());
        carsTypePopWindow.setBgView(binding.grayLayout);
        carsTypePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3) type;
                typePosition = (int) key2;
                pickResultIndustry.setNo(entity.getNo());
                binding.rvPackerChangeFirstContentTitle.setScrollPosition(typePosition,0,true);
                initFilterData(false);
            }
        });
        carsTypePopWindow.setOnDismissListener(() -> binding.ivEmploymentAll.setImageResource(R.mipmap.btn_expand_team_employment));
    }

    @Override
    protected void init() {
        if ("".equals(pickResultProvince.getNo())){

        }
        initFilterData(false);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_employment_all){
            if (carsTypeData != null && carsTypeData.size() > 0){
                carsTypePopWindow.setData(carsTypeData,typePosition);
                carsTypePopWindow.showPopwindow(binding.ivEmploymentAll, 0, 0);
            }
            binding.ivEmploymentAll.setImageResource(R.mipmap.btn_expand_up_gray);
        }else if (id == R.id.tv_worker_status) {
            if (mEmploymentTypePopup1 == null) {
                viewModel.getEmploymentStatus();
            } else {
                setWorkerStatusData(null);
            }
        } else if (id == R.id.tv_area) {
            if (popCity == null) {
                viewModel.getCityList();
            } else {
                showCityPop(null);
            }
        }
    }

    public void showList(List<EmploymentEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                adapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
                binding.recyclerView.smoothScrollToPosition(0);
            } else {
                adapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void showWorkTypePop(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data) {
        int index = 0;
        mWorkType = data;
        if (data != null && data.size() > 0) {
            for (int i = 0; i < data.size(); i++) {
                PickFilterEntity datum = data.get(i);
                GoodsInfoTypeEntity3 info = new GoodsInfoTypeEntity3();
                info.setName(datum.getName());
                info.setNo(datum.getNo());
                carsTypeData.add(info);
                if (!TextUtils.isEmpty(pickResultIndustry.getName()) && pickResultIndustry.getName().equals(datum.getName())) {
                    index = i;
                }
                binding.rvPackerChangeFirstContentTitle.addTab(binding.rvPackerChangeFirstContentTitle.newTab().setText(datum.getName()));
            }
        }
        if (index != 0) {
            int finalIndex = index;
            typePosition = index;
            binding.rvPackerChangeFirstContentTitle.postDelayed(() -> binding.rvPackerChangeFirstContentTitle.getTabAt(finalIndex).select(), 100);
        }
    }

    public void setWorkerStatusData(List<PickSelectEntity> data) {
        if (mEmploymentTypePopup1 == null) {
            int position = 0;
            mEmploymentTypePopup1 = new PickPopWindow1(this);
            mEmploymentTypePopup1.setBgView(binding.grayLayout);
            if (!"".equals(pickResultWorkerStatus.getNo())){
                for (int i = 0; i < data.size(); i++) {
                    if (pickResultWorkerStatus.getNo().equals(data.get(i).getNo())){
                        position = i;
                    }
                }
            }
            mEmploymentTypePopup1.setOnResultClick((PickPopWindow1.OnResultClick) (key1, key2, key3) -> {
                pickResultWorkerStatus.setNo((String) key1);
                pickResultWorkerStatus.setName((String) key2);
                pickResultWorkerStatus.setPosition((int) key3);

                initFilterData(false);
            });
            mEmploymentTypePopup1.setOnDismissListener(() -> {

            });

            mEmploymentTypePopup1.setData(data);
            mEmploymentTypePopup1.setDefaultItem(position);
        }

    }

    public void showCityPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (popCity == null) {
            popCity = new ThreeLevelAreaWindow(getActivity()) {
                @Override
                protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.provinceClick(baseAreaEntity, position);
                    pickResultProvince.setNo(baseAreaEntity.getNo());
                    pickResultProvince.setName(baseAreaEntity.getSimpleName());
                    if (getString(R.string.app_all).equals(baseAreaEntity.getSimpleName()) || pickResultProvince.getName().equals(baseAreaEntity.getSimpleName())) {
                        pickResultCity.setNo("");
                        pickResultCity.setName("");
                        pickResultArea.setNo("");
                        pickResultArea.setName("");
                    }
                }

                @Override
                protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.cityClick(baseAreaEntity, position);
                    pickResultCity.setNo(baseAreaEntity.getNo());
                    pickResultCity.setName(baseAreaEntity.getSimpleName());
                    if (getString(R.string.app_all).equals(baseAreaEntity.getSimpleName()) || pickResultCity.getName().equals(baseAreaEntity.getSimpleName())) {
                        pickResultArea.setNo("");
                        pickResultArea.setName("");
                    }
                }

                @Override
                protected void areaClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.areaClick(baseAreaEntity, position);
                    pickResultArea.setNo(baseAreaEntity.getNo());
                    pickResultArea.setName(baseAreaEntity.getSimpleName());
                }
            };
            popCity.setBgView(binding.grayLayout);
            popCity.setShowSimpleName(true);
            popCity.setProvinceSingle();
            popCity.setCitySingle();
            popCity.setAreaSingle();
            pickResultProvince.getNo();
            popCity.setOnResultClick((key1, key2, key3) -> initFilterData(false));
            popCity.setOnDismissListener(() -> {

            });

            if (!"".equals(pickResultCity.getNo())){
                popCity.setDataForTeamMainPage(data, true, pickResultProvince.getNo(),pickResultCity.getNo(),pickResultArea.getNo());
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            if (requestCode == 0) {
                initFilterData(false);
            }
        }
    }
}
